<template>
  <div style="margin: 10px;">
    <!-- 搜索表单 -->
    <el-form :inline="true" :model="params.query" class="demo-form-inline">
      <el-form-item label="字典编码">
        <el-input v-model="params.query.code" placeholder="请输入字典编码"></el-input>
      </el-form-item>
      <el-form-item label="字典描述">
        <el-input v-model="params.query.description" placeholder="请输入字典描述"></el-input>
      </el-form-item>
      <el-form-item label="是否生效">
        <el-select v-model="params.query.enableFlag" placeholder="是否生效">
          <el-option label="生效" value="1"></el-option>
          <el-option label="无效" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button type="primary" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="addRow">新增</el-button>
      </el-form-item>
    </el-form>

    <!--分割线 -->
    <el-divider></el-divider>

    <!-- 数据显示区域 -->
    <el-table
      :data="history"
      style="width: 100%"
      height="550"
      border
      highlight-current-row
      :default-sort="{prop: 'updateTime', order: 'descending'}"
    >
      <el-table-column type="index" width="50" align="center"></el-table-column>
      <el-table-column prop="code" label="字典编码" align="center" sortable></el-table-column>
      <el-table-column prop="value" label="字典值" align="center"></el-table-column>
      <el-table-column prop="description" label="字典描述" align="center"></el-table-column>
      <el-table-column
        prop="enableFlag"
        label="是否生效"
        align="center"
        :formatter="enableFlagFormatter"
      ></el-table-column>
      <el-table-column prop="updateTime" label="更新时间" align="center" sortable></el-table-column>
      <el-table-column prop="operation" label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="success"
            size="mini"
            @click="updateStatus(scope.row)"
            v-if="scope.row.enableFlag === 0"
          >启用</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="updateRow(scope.row)"
            v-if="scope.row.enableFlag === 1"
          >更新</el-button>
          <el-button
            type="primary"
            size="mini"
            @click="delRow(scope.row)"
            v-if="scope.row.enableFlag === 1"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      @size-change="pageSizeChange"
      @current-change="pageNowChange"
      :current-page="params.query.pageNow"
      :page-sizes="[10, 15, 20]"
      :page-size="params.query.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="params.rowCount"
    ></el-pagination>

    <!-- 新增字典弹框: 注意父组件传递给子组件数据，属性名必须-替代驼峰命名, closeAddDialog 子组件回调函数 -->
    <add_update_dictionary
      :add-dialog-visible="addDialogVisible"
      :dictionary-type-array="dictionaryTypeArray"
      :dict-param="dictParam"
      :dialog-title="addDiaglogTitle"
      :operation="addOperation"
      v-on:closeAddDialog="closeAddDialog"
      v-on:search="search"
    ></add_update_dictionary>

     <!-- 更新 -->
    <add_update_dictionary
      :add-dialog-visible="updateDialogVisible"
      :dictionary-type-array="dictionaryTypeArray"
      :dict-param="dictParam"
      :dialog-title="updateDiaglogTitle"
      :operation="updateOperation"
      v-on:closeAddDialog="closeAddDialog"
      v-on:search="search"
    ></add_update_dictionary>
  </div>
</template>

<script>
import {
  addDictionary,
  queryPublishDictByPage,
  queryPublishDictByType,
  updateDictionary
} from "@/components/api/dictionary";
import Constants from "@/components/util/Constants";
import AddUpdateDictionary from "@/components/view/dictionary/AddUpdateDictionary";
import GlobalUtil from "@/components/util/GlobalUtil";

export default {
  name: "ProjectBaseConfig",
  components: {
    add_update_dictionary: AddUpdateDictionary
  },
  data: function() {
    return {
      params: {
        query: {
          code: "",
          description: "",
          enableFlag: "1",
          pageNow: 1,
          pageSize: 10
        },
        pageCount: 0,
        rowCount: 0
      },
      dictParam: {
        uuid:'',
        code: "",
        value: "",
        parentType: "",
        description: "",
        enableFlag: "1"
      },
      addOperation: Constants.ADD_STR,
      updateOperation: Constants.UPDATE_STR,
      addDiaglogTitle: Constants.ADD,
      updateDiaglogTitle: Constants.UPDATE,
      history: [],
      addDialogVisible: false,
      updateDialogVisible: false,
      dictionaryTypeArray: []
    };
  },
  methods: {
    resetQuery() {
      this.params.query = {
        code: "",
        description: "",
        enableFlag: "1",
        pageNow: 1,
        pageSize: 10
      };
    },
    search: function() {
      queryPublishDictByPage(this.params.query, data => {
        if (data) {
          this.history = data.data;
          this.params.pageCount = data.pageCount;
          this.params.rowCount = data.rowCount;
        }
      });
    },
    addRow() {
      this.addDialogVisible = true;
    },
    updateRow(row) {
      this.dictParam = GlobalUtil.cloneObject(row);
      this.dictParam.enableFlag = ''.concat(row.enableFlag);
      this.updateDialogVisible = true;
    },
    updateStatus(row) {
      let dictionary = { uuid: row.uuid, enableFlag: 1 };
      updateDictionary(dictionary, () => {
        this.search();
      });
    },
    delRow(row) {
      let deleteParam = { uuids: [row.uuid] };
      this.$confirm("确定是否删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let dictionary = { uuid: row.uuid, enableFlag: 0 };
          updateDictionary(dictionary, () => {
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    queryDictionayType() {
      queryPublishDictByType(data => {
        if (data) {
          this.dictionaryTypeArray = data;
        }
      });
    },
    closeAddDialog() { //关闭对话框，数据初始化
      this.dictParam = {
        code: "",
        value: "",
        parentType: "",
        description: "",
        enableFlag: "1"
      };
      this.addDialogVisible = false;
      this.updateDialogVisible = false;
    },
    enableFlagFormatter(row) {
      if (row.enableFlag === 1) {
        return Constants.TRUE;
      }
      return Constants.FALSE;
    },
    pageSizeChange(pageSize) {
      this.params.query.pageSize = pageSize;
      this.search();
    },
    pageNowChange(pageNow) {
      this.params.query.pageNow = pageNow;
      this.search();
    }
  },
  mounted() {
    this.search();
    this.queryDictionayType();
  }
};
</script>

<style scoped>
</style>
